
package ru.cod.rs.interfaces.impl;

import java.io.IOException;
import java.io.InputStream;
import ru.cod.rs.interfaces.Graphics;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class GraphicsImpl implements Graphics {
    
    private Bitmap frame;
    
    private Canvas canvas;
    
    private AssetManager manager;
    
    public GraphicsImpl(AssetManager assets, Bitmap frame) {
    
        this.frame = frame;
        manager = assets;
        canvas = new Canvas(this.frame);
    }
    
    @Override
    public Bitmap newBitmap(String fileName) {
    
        InputStream in = null;
        Bitmap bm = null;
        try {
            in = manager.open(fileName);
            bm = BitmapFactory.decodeStream(in);
            if (bm == null) {
                throw new RuntimeException("No file " + fileName);
            }
        } catch (IOException e) {
            
        } finally {
            if (in != null) {
                try {
                    in.close();
                    
                } catch (IOException e) {
                }
            }
        }
        
        return bm;
    }
    
    @Override
    public void drawBitmap(Bitmap bm, int x, int y) {
    
        canvas.drawBitmap(bm, x, y, null);
    }
    
    @Override
    public void clear(int color) {
    
        canvas.drawColor(color);
    }
    
    @Override
    public int getWidth() {
    
        // TODO Auto-generated method stub
        return frame.getWidth();
    }
    
    @Override
    public int getHeight() {
    
        // TODO Auto-generated method stub
        return frame.getHeight();
    }
    
    @Override
    public void drawText(String text, int x, int y, int color) {
    
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setTextSize(16);
        canvas.drawText(text, x, y, paint);
    }
}
